﻿using System;
using System.Web;
using yiCommerce.Core;
using yiCommerce.Core.Domain;
using yiCommerce.Core.Domain.Customers;
using yiCommerce.Core.Domain.Payments;
using yiCommerce.Core.Domain.Shops;

namespace yiCommerce.Admin
{
    public class WebWorkContext:IWorkContext
    {
        private const string CustomerCookieName = "CSMF.user";
        //private AuthenticationService _authenticationService;
        //private UserService _userService;
        //private UserInfo _cachedCustomer;
        public WebWorkContext()
        {
            //_authenticationService = new AuthenticationService();
            //_userService = new UserService();
        }


        public Customer CurrentCustomer
        {
            get
            {
                return GetCurrentUser();
            }
            set
            {
                //SetCustomerCookie(value.UserGuid);
                //_cachedCustomer = value;
            }
        }

        protected Customer GetCurrentUser()
        {

            //if (_cachedCustomer != null)
            //    return _cachedCustomer;

            //Customer customer = null;
            //if (HttpContext.Current != null)
            //{

            //    //registered user
            //    if (customer == null || customer.Deleted || !customer.Active)
            //    {
            //        customer = _authenticationService.GetAuthenticatedCustomer();
            //    }

              

            //    //load guest customer
            //    if (customer == null || customer.Deleted || !customer.Active)
            //    {
            //        var customerCookie = GetCustomerCookie();
            //        if (customerCookie != null && !String.IsNullOrEmpty(customerCookie.Value))
            //        {
            //            Guid customerGuid = new Guid(customerCookie.Value);

            //            var customerByCookie = _userService.GetUserByGuid(customerGuid);
            //            if (customerByCookie != null &&
            //                //this customer (from cookie) should not be registered
            //                !customerByCookie.IsRegistered() )
            //                customer = customerByCookie;
            //        }
            //    }

            //    //create guest if not exists
            //    if (customer == null || customer.Deleted || !customer.Active)
            //    {
            //        customer = _userService.InsertGuestCustomer();
            //    }

            //    SetCustomerCookie(customer.UserGuid);
            //}


            //_cachedCustomer = customer;
            //return customer;
            return new Customer()
                {
                    Shop = new Shop() {Id = 3}
                };
        }

        private void SetCustomerCookie(Guid userGuid)
        {
            var cookie = new HttpCookie(CustomerCookieName);
            cookie.Value = userGuid.ToString();
            if (userGuid== Guid.Empty)
            {
                cookie.Expires = DateTime.Now.AddMonths(-1);
            }
            else
            {
                int cookieExpires = 24 * 365; //TODO make configurable
                cookie.Expires = DateTime.Now.AddHours(cookieExpires);
            }
            var _httpContext = HttpContext.Current;

            if (_httpContext != null && _httpContext.Response != null)
            {
                _httpContext.Response.Cookies.Remove(CustomerCookieName);
                _httpContext.Response.Cookies.Add(cookie);
            }
        }

        private HttpCookie GetCustomerCookie()
        {
            if (HttpContext.Current == null || HttpContext.Current.Request == null)
                return null;

            return HttpContext.Current.Request.Cookies[CustomerCookieName];
        }

        public bool IsAdmin { get; set; }
        public SystemSetting SystemSetting { get; set; }
        public AlipayPaymethodSetting AlipayPaymethodSetting { get; set; }
    }
}